package com.cgtong.android.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.cgtong.android.R;
import com.cgtong.android.adapter.ProblemCountMonthYearAdapter;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.bean.MonthYearReportBean;
import com.cgtong.android.callback.EmptyCallback;
import com.cgtong.android.callback.ErrorCallback;
import com.cgtong.android.mvp.contract.MonthReportContract;
import com.cgtong.android.mvp.presenter.MonthReportPresenter;
import com.cgtong.android.util.PostUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhouyou.http.exception.ApiException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MonthReportActivity extends BaseTitleActivity implements MonthReportContract.View {
    private static final String KEY_YEAR = "year";

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeLayout;

    private MonthReportContract.Presenter mPresenter;
    private ProblemCountMonthYearAdapter mAdapter;

    private List<MonthYearReportBean> mList;

    private String mYear;
    private int mYearInt;

    public static void start(Context context, String dateTime) {
        Intent starter = new Intent(context, MonthReportActivity.class);
        starter.putExtra(KEY_YEAR, dateTime);
        context.startActivity(starter);
    }

    @Override
    protected String getContentTitle() {
        return getString(R.string.title_month_report);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_month_report;
    }

    @Override
    protected void loadDataByNet() {
        mPresenter = new MonthReportPresenter(this);
        initRecyclerView();
        mList = new ArrayList<>();

        mYear = getIntent().getStringExtra(KEY_YEAR);

        mTitleCenterTxt.setText(mYear + getString(R.string.title_month_report));

        int yearInt = Integer.parseInt(mYear);

        mYearInt = yearInt + 1;

        mYear = String.valueOf(mYearInt) + "-01";
        LogUtils.d("年：" + mYear);
        mPresenter.httpMonthReportData(mYear, true);

    }

    @Override
    protected void onDataReloadByNet(View v) {
        mPresenter.httpMonthReportData(mYear, true);
    }

    /**
     * 初始化列表
     */
    private void initRecyclerView() {
        mSwipeLayout.setColorSchemeResources(R.color.color_scheme);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.httpMonthReportData(mYear, true);

            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(MonthReportActivity.this));
        mAdapter = new ProblemCountMonthYearAdapter(mList);
        mAdapter.setMonthReport(true);
        mRecyclerView.setAdapter(mAdapter);
//        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL));
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                String maxTime = mAdapter.getItem(mAdapter.getItemCount() - 2).getDatetime();
                mPresenter.httpMonthReportData(maxTime, false);
            }
        }, mRecyclerView);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TimeLimitActivity.start(MonthReportActivity.this, mAdapter.getItem(position).getDatetime());

            }
        });
    }


    //--------回调---------

    @Override
    public void setRefreshing(boolean isRefresh) {
        if(mSwipeLayout!=null){
            mSwipeLayout.setRefreshing(isRefresh);
        }
    }

    @Override
    public void setEnableLoadMore(boolean enableLoadMore) {
        mAdapter.setEnableLoadMore(enableLoadMore);
    }

    @Override
    public void onLoadMoreEnd(boolean isLoadMoreEnd) {
        mAdapter.loadMoreEnd(isLoadMoreEnd);
    }

    @Override
    public void onNullData() {
        PostUtil.postCallbackDelayed(loadService, EmptyCallback.class);
    }

    @Override
    public void onHttpMonthReportDataSuccess(List<MonthYearReportBean> datas, boolean isRefresh) {
        PostUtil.postSuccessDelayed(loadService);
        if (isRefresh) {
            mAdapter.setNewData(datas);
        } else {
            mAdapter.addData(datas);
        }
        mAdapter.loadMoreComplete();
    }


    @Override
    public void onError(ApiException e) {
        PostUtil.postCallbackDelayed(loadService, ErrorCallback.class);
        ToastUtils.showShort(e.getMessage());
    }


}
